<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件


function readFileNames1($dirPath)
{
    // 打开目录，然后读取其内容
    if (is_dir($dirPath)) {
        $files = array();
        if ($dh = opendir($dirPath)) {
            while (($file = readdir($dh)) !== false) {
                $files[] = $file;
            }
            $files = removeNoNeedFile($files);
            closedir($dh);
        }
        return $files;
    }
    return false;
}


/**
 * 0= windows 1=其他
 * @title getSystemType
 * @description
 * @author: daniel
 */
function isWindows()
{
    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
        return 1;
    } else {
        return 0;
    }
}

function tryUrlEncode($data)
{
    if (isWindows()) {
        return urlencode($data);
    } else {
        return $data;
    }
}

function tryUrlDecode($data)
{
    if (strpos($data, "%") !== false) {
        return urldecode($data);
    } else {
        return $data;
    }
}

function readFileNames($dirPath)
{
    if (isWindows()) {
        $dirPath = tryConvertToGBK($dirPath);
    }
    // 打开目录，然后读取其内容
    if (is_dir($dirPath)) {
        $files = scandir($dirPath);
        if (isWindows()) {
            $files = tryConvertToUTF8($files);
        }
        return removeNoNeedFile($files);
    }
    return false;
}


function tryConvertToGBK($data)
{
    if (is_array($data)) {
        foreach ($data as &$item) {
            $item = tryConvertToGBK($item);
        }
        return $data;
    } else {
        $encode = mb_detect_encoding($data, array("ASCII", 'UTF-8', "GB2312", "GBK"));
        if ($encode == "UTF-8") {
            return iconv("UTF-8", "GBK", $data);
        }
    }
    return $data;
}

function tryConvertToUTF8($data)
{
    if (is_array($data)) {
        foreach ($data as &$item) {
            $item = tryConvertToUTF8($item);
        }
        return $data;
    } else {
        $encode = mb_detect_encoding($data, array("ASCII", "GBK", 'UTF-8', "GB2312"));
        if (in_array($encode, ["GBK", "CP936", "GB2312"])) {
            return iconv("GBK", "UTF-8", $data);
        }
    }
    return $data;
}

/**
 * @title removeNoNeedFile
 * @description 移除文件数组中的. 以及 ..
 * @author: daniel
 * @param $files
 * @return mixed
 */
function removeNoNeedFile($files)
{
    foreach ($files as $key => $file) {
        if ($file == "." || $file == "..") {
            unset($files[$key]);
        }
    }
    return $files;
}


/**
 * @title buildResponse
 * @description 构建ajax请求默认返回结构
 * @author: daniel
 * @param int $code 使用RESULT_SUCCESS与RESULT_ERROR做参数
 * @param $msg
 * @param $data
 */
function buildResponse($code = 200, $msg = '', $data = [])
{

    $returnData["code"] = $code;
    $returnData["msg"] = $msg;
    $returnData["data"] = $data;
    return $returnData;
}

/**
 * @title buildResponse
 * @description 构建ajax请求默认返回结构
 * @author: daniel
 * @param $msg
 * @param $data
 */
function buildSuccess($msg = '操作成功!', $data = [], $echo = false)
{
    if ($echo) {
        echo json_encode(buildResponse(200, $msg, $data));
    } else {
        return buildResponse(200, $msg, $data);
    }
}

/**
 * @title buildResponse
 * @description 构建ajax请求默认返回结构
 * @author: daniel
 * @param $msg
 * @param $data
 */
function buildError($msg = '操作失败!', $data = [], $echo = false)
{
    if ($echo) {
        echo json_encode(buildResponse(400, $msg, $data));
    } else {
        return buildResponse(400, $msg, $data);
    }
}

function mod5file()
{

}


/*
 * 下划线转驼峰
 */
function convertUnderline($str)
{
    $str = preg_replace_callback('/([-_]+([a-z]{1}))/i', function ($matches) {
        return strtoupper($matches[2]);
    }, $str);
    return $str;
}

/*
 * 驼峰转下划线
 */
function humpToLine($str)
{
    $str = preg_replace_callback('/([A-Z]{1})/', function ($matches) {
        return '_' . strtolower($matches[0]);
    }, $str);
    if(strpos($str,"_")===0)
    {
        return substr($str,1);
    }
    return $str;
}
